package cn.com.allunion.sms.service.mybatis.impl;

import cn.com.allunion.common.UUIDGenerator;
import cn.com.allunion.common.page.PageView;
import cn.com.allunion.common.page.QueryResult;
import cn.com.allunion.sms.mapper.SmsoperatorMapper;
import cn.com.allunion.sms.pojo.Smsoperator;
import cn.com.allunion.sms.pojo.SmsoperatorExample;
import cn.com.allunion.sms.service.mybatis.SmsoperatorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 运营商操作业务逻辑接口实现类
 * Created by YangJie on 2016/4/18.
 */
@Service
@Transactional
public class SmsoperatorServiceImpl implements SmsoperatorService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SmsoperatorServiceImpl.class) ;

    @Autowired
    SmsoperatorMapper smsoperatorMapper ;

    @Override
    public String addNewOperator(String smsOperatorName, int priority, String joinNum, String joinState) {
        Smsoperator smsOperator = new Smsoperator();
        smsOperator.setSmsOperatorID(UUIDGenerator.generatorRandomUUID());
        smsOperator.setSmsOperatorName(smsOperatorName);
        smsOperator.setPriority(priority);
        smsOperator.setJoinNum(joinNum);
        smsOperator.setJoinState(joinState);
        int result = smsoperatorMapper.insert(smsOperator) ;
        if (result > 0) {
            return "success";
        }

        return "failure" ;
    }

    @Override
    public String udpateOperator(String id, String smsOperatorName, int priority, String joinNum, String joinState) {
        Smsoperator smsOperator = smsoperatorMapper.selectByPrimaryKey(id) ;
        if (smsOperator != null) {
            smsOperator.setSmsOperatorName(smsOperatorName);
            smsOperator.setPriority(priority);
            smsOperator.setJoinNum(joinNum);
            smsOperator.setUpdateDate(new Date());
            smsOperator.setJoinState(joinState);
            int result = smsoperatorMapper.updateByPrimaryKey(smsOperator) ;
            if (result > 0) {
                return "success";
            }

            return "failure" ;
        }
        return "error";
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<Smsoperator> findAll() {
        // 条件为空的时候取全部
        SmsoperatorExample example = new SmsoperatorExample() ;
        return smsoperatorMapper.selectByExample(example);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public QueryResult<Smsoperator> findAll(int pageCurrent, int pageSize) {
        SmsoperatorExample example = new SmsoperatorExample() ;
        PageHelper.startPage(pageCurrent, pageSize) ;
        List<Smsoperator> smsoperators = smsoperatorMapper.selectByExample(example);

        PageInfo<Smsoperator> pageInfo = new PageInfo<>(smsoperators) ;
        QueryResult<Smsoperator> queryResult = new QueryResult<>(smsoperators, pageInfo.getTotal()) ;
        return queryResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Smsoperator findById(String id) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("id is {}", id);
        }
        if (StringUtils.isBlank(id)) {
            LOGGER.warn("id is null.");
            return null ;
        }

        Smsoperator smsoperator = smsoperatorMapper.selectByPrimaryKey(id);

        return smsoperator;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Smsoperator findOperatorObj(String joinNum) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("joinNum is {}", joinNum);
        }
        if (StringUtils.isBlank(joinNum)) {
            LOGGER.warn("joinNum is null.");
            return null ;
        }
        SmsoperatorExample example = new SmsoperatorExample() ;
        SmsoperatorExample.Criteria criteria = example.createCriteria();
        criteria.andJoinNumEqualTo(joinNum) ;
        List<Smsoperator> smsoperators = smsoperatorMapper.selectByExample(example);

        if (null != smsoperators && smsoperators.size()>0) {
            return smsoperators.get(0) ;
        }
        return null;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Smsoperator shenma(String joinNum) {
        if (StringUtils.isBlank(joinNum)) {
            LOGGER.warn("joinNum is null.");
            return null ;
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("joinNum is {}", joinNum);
        }
        return findOperatorObj(joinNum);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Smsoperator findbyName(String smsOperatorName) {
        if (StringUtils.isBlank(smsOperatorName)) {
            LOGGER.warn("smsOperatorName is null.");
            return null ;
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("smsOperatorName is {}", smsOperatorName);
        }
        SmsoperatorExample example = new SmsoperatorExample() ;
        SmsoperatorExample.Criteria criteria = example.createCriteria();
        criteria.andSmsOperatorNameEqualTo(smsOperatorName) ;
        List<Smsoperator> smsoperators = smsoperatorMapper.selectByExample(example);

        if (null != smsoperators && smsoperators.size()>0) {
            return smsoperators.get(0) ;
        }
        return null;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public QueryResult<Smsoperator> getScrollData(int pageCurrent, int pageSize) {
        SmsoperatorExample example = new SmsoperatorExample() ;
        PageHelper.startPage(pageCurrent, pageSize) ;
        List<Smsoperator> smsoperators = smsoperatorMapper.selectByExample(example);

        PageInfo<Smsoperator> pageInfo = new PageInfo<>(smsoperators) ;
        QueryResult<Smsoperator> queryResult = new QueryResult<>(smsoperators, pageInfo.getTotal()) ;
        return queryResult;
    }
}
